﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Collections;
using System.Windows.Forms.DataVisualization.Charting;

namespace _0413MyTimer
{
    public partial class Form1 : Form
    {
        Random random = new Random(0);
        ArrayList datalist = new ArrayList();

        //是否启用局部放大,FALSE--未启用
        bool flag = false;

        public Form1()
        {
            InitializeComponent();

            timer1.Interval = 250;

            chart1.Series[0].ChartType = SeriesChartType.Line;
            chart1.Series[0].IsValueShownAsLabel = false;//图上不显示数据点的值
            chart1.Series[0].ToolTip = "#VALX,#VALY";//鼠标停留在数据点上，显示XY值

            //启用X游标，以支持局部区域选择放大
            chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
            chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
            chart1.ChartAreas[0].CursorX.LineColor = Color.Pink;
            chart1.ChartAreas[0].CursorX.IntervalType = DateTimeIntervalType.Auto;
            chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = false;
            chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;//启用X轴滚动条按钮
            //chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = false;
            //chart1.ChartAreas[0].CursorY.IsUserEnabled = true;
            //chart1.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
            //chart1.ChartAreas[0].CursorY.LineColor = Color.OrangeRed;
            //chart1.ChartAreas[0].CursorY.Interval = 0.1;
            //chart1.ChartAreas[0].CursorX.SelectionColor = Color.Transparent;
            //chart1.ChartAreas[0].CursorY.SelectionColor = Color.Transparent;

            comboBox1.Items.AddRange(new object[] { "OverView" , "Follow" });
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender , EventArgs e)
        {
            timer1.Start();
        }

        private void button2_Click(object sender , EventArgs e)
        {
            switch (((Button)sender).Text)
            {
                case "暂停":
                    timer1.Stop();
                    ((Button)sender).Text = "继续";
                    break;
                case "继续":
                    timer1.Start();
                    ((Button)sender).Text = "暂停";
                    break;
            }
            //timer1.Stop();
        }

        private void timer1_Tick(object sender , EventArgs e)
        {
            //添加数据
            datalist.Add(random.Next(0 , 100));
            chart1.Series[0].Points.AddXY(datalist.Count , datalist[datalist.Count - 1]);

            //启用局部放大
            if (flag)
            {
                this.textBox1.Text = chart1.ChartAreas[0].AxisX.ScaleView.Size.ToString();
                this.textBox2.Text = chart1.ChartAreas[0].AxisX.ScaleView.Position.ToString();
                return;
            }
            
            //未启动局部放大
            if (comboBox1.SelectedItem.ToString() == "OverView")
            {
                chart1.ChartAreas[0].AxisX.ScaleView.Position = 1;
                if (datalist.Count > 10)
                {
                    double max = chart1.ChartAreas[0].AxisX.Maximum;
                    max = (datalist.Count / 10 + 1) * 10;
                    chart1.ChartAreas[0].AxisX.Interval = max / 10;
                }
                chart1.ChartAreas[0].AxisX.ScaleView.Size = datalist.Count * 1.1;
            }
            if (comboBox1.SelectedItem.ToString() == "Follow")
            {
                chart1.ChartAreas[0].AxisX.Interval = 1D;
                chart1.ChartAreas[0].AxisX.ScaleView.Size = 8D;
                if (datalist.Count <= chart1.ChartAreas[0].AxisX.ScaleView.Size)
                    chart1.ChartAreas[0].AxisX.ScaleView.Position = 1;
                else
                    chart1.ChartAreas[0].AxisX.ScaleView.Position = datalist.Count - chart1.ChartAreas[0].AxisX.ScaleView.Size;
            }

            this.textBox1.Text = chart1.ChartAreas[0].AxisX.ScaleView.Size.ToString();
            this.textBox2.Text = chart1.ChartAreas[0].AxisX.ScaleView.Position.ToString();
            
        }


        private void button3_Click(object sender , EventArgs e)
        {
            if (comboBox1.Items.Contains("Zoom"))
            {
                comboBox1.Items.Remove("Zoom");
                comboBox1.SelectedIndex = 0;
                flag = false;
            }
            timer1.Stop();
            datalist.Clear();
            chart1.Series[0].Points.Clear();
            chart1.ChartAreas[0].CursorX.SelectionStart = chart1.ChartAreas[0].CursorX.SelectionEnd = 0;//删除局部放大范围线
            button2.Text = "暂停";
        }

        //图表区域局部放大
        private void chart1_SelectionRangeChanged(object sender , CursorEventArgs e)
        {
            //无数据时返回
            if (chart1.Series[0].Points.Count == 0)
                return;
            
            double start_position = 0.0;
            double end_position = 0.0;
            double myInterval = 0.0;
            start_position = e.NewSelectionStart;
            end_position = e.NewSelectionEnd;
            myInterval = Math.Abs(start_position - end_position);
            if (myInterval == 0.0)
                return;

            //X轴视图起点
            chart1.ChartAreas[0].AxisX.ScaleView.Position = Math.Min(start_position , end_position);
            //X轴视图长度
            chart1.ChartAreas[0].AxisX.ScaleView.Size = myInterval;
            //X轴间隔
            if (myInterval<11.0)
            {
                chart1.ChartAreas[0].AxisX.Interval = 1;
            }
            else
            {
                chart1.ChartAreas[0].AxisX.Interval = Math.Floor(myInterval / 10);
            }
            flag = true;
            if (!comboBox1.Items.Contains("Zoom"))
            {
                comboBox1.Items.Add("Zoom");
                comboBox1.SelectedItem = "Zoom";
            }
            
        }


        private void comboBox1_SelectedIndexChanged(object sender , EventArgs e)
        {
            if (comboBox1.SelectedItem.ToString() == "Zoom")
            {
                flag = true;
            }
            else if (comboBox1.SelectedItem.ToString() == "OverView" || comboBox1.SelectedItem.ToString() == "Follow")
            {
                comboBox1.Items.Remove("Zoom");
                flag = false;
            }
        }

        //局部放大后，恢复视图
        private void chart1_AxisScrollBarClicked(object sender , ScrollBarEventArgs e)
        {
            if (e.ButtonType==ScrollBarButtonType.ZoomReset)
            {
                chart1.ChartAreas[0].AxisX.Interval = 0;
            }
        }
    }
}
